package cn.domypay.back.control.impl;

import java.util.List;
import java.util.Properties;

import cn.domypay.back.base.BaseObj;
import cn.domypay.back.bean.DmpOrderInfo;
import cn.domypay.back.biz.DmpOrderInfoBiz;
import cn.domypay.back.biz.DmpPerpaidLogBiz;
import cn.domypay.back.conf.OrderContants;
import cn.domypay.back.conf.SysContants;
import cn.domypay.back.control.OrderControl;
import cn.domypay.back.exception.DoMyPayException;
import cn.domypay.back.log.DoMyPayLogger;
import cn.domypay.back.service.AuthImpl;
import cn.domypay.back.service.AuthImplServiceClient;
import cn.domypay.back.service.SHJbywImpl;
import cn.domypay.back.service.SHJbywImplServiceClient;
import cn.domypay.back.utils.ReadProperties;
import cn.domypay.back.utils.StringUtils;

public class OrderControlImpl implements OrderControl{
	
	private final static DoMyPayLogger logger = DoMyPayLogger.getLogger(OrderControlImpl.class);
	
	private DmpOrderInfoBiz dmpOrderInfoBiz;
	
	private DmpPerpaidLogBiz dmpPerpaidLogBiz;
	
	private String strAuthInfo;
	
	public DmpOrderInfoBiz getDmpOrderInfoBiz() {
		return dmpOrderInfoBiz;
	}

	public void setDmpOrderInfoBiz(DmpOrderInfoBiz dmpOrderInfoBiz) {
		this.dmpOrderInfoBiz = dmpOrderInfoBiz;
	}

	public DmpPerpaidLogBiz getDmpPerpaidLogBiz() {
		return dmpPerpaidLogBiz;
	}

	public void setDmpPerpaidLogBiz(DmpPerpaidLogBiz dmpPerpaidLogBiz) {
		this.dmpPerpaidLogBiz = dmpPerpaidLogBiz;
	}

	/**
	 * 获取已付款，但是没有发货的订单集合
	 * @throws DoMyPayException 
	 */
	public List<BaseObj> getAcceptOrderList() throws DoMyPayException
	{
		return dmpOrderInfoBiz.getAllOrderByStatus(OrderContants.ORDER_STATUS_ACCEPTTED, OrderContants.ORDER_GOODS_TYPE);
	}
	
	/**
	 * 处理订单充值
	 * @throws DoMyPayException 
	 * @return 充值成功返回true,失败返回false
	 */
	public boolean dealPerPaid(DmpOrderInfo dmpOrderInfo) throws DoMyPayException,Exception
	{
		
		Properties prop = ReadProperties.init(SysContants.DEFAULT_PROPERTIES_FILE);
		//登陆webservice
		AuthImplServiceClient authImplServiceClient = new AuthImplServiceClient();
		
		AuthImpl authImpl = authImplServiceClient.getIAuth();
		try{
			strAuthInfo = authImpl.requestAuthToken(prop.getProperty("perpaid.userId"), prop.getProperty("perpaid.password"));
			
			logger.logInfo(strAuthInfo);
		}catch(Exception e){
			
			//当登录连接出错，发送一封提醒邮件，并停止系统
			//System.exit(0);
			throw new Exception();
		}
		
		if(strAuthInfo.length() <= SysContants.SERVICES_ERROR_LENGTH)
		{
			//如果登录返回的验证长字符串长度小于4，发送一封提醒邮件，并停止系统
			//System.exit(0);
			throw new DoMyPayException("商户登录验证错误");
		}
		
		SHJbywImplServiceClient jbywImplServiceClient = new SHJbywImplServiceClient();
		
		SHJbywImpl jbywImpl = jbywImplServiceClient.getISHJbyw();
		
		String result = jbywImpl.ydCZ(strAuthInfo, dmpOrderInfo.getOrderSn(), 
				prop.getProperty("perpaid.terminalNo"), dmpOrderInfo.getMobilePhone(),
				dmpOrderInfo.getMarketPrice(), prop.getProperty("perpaid.dealPwd"));
		
		//如果充值成功,返回的结果代码是: 充值ID流水号,000
		String results[] = StringUtils.parsePerpaidRest(result);
		logger.logInfo(results.toString());
		if(null != results)
		{
			if(OrderContants.ORDER_SUCCESS_RESULT.equals(results[1]))
			{
				//保存充值返回信息
				dmpPerpaidLogBiz.addDmpPerpaidLog(dmpOrderInfo, results, prop);
				return true;
			}else{
				//保存错误充值错误信息:需要对不同的错误信息，产生对应的处理方式
				
				throw new DoMyPayException();
			}
		}
		
		return false;
	}
	
	
	public void modityOrderStatus(DmpOrderInfo dmpOrderInfo) throws DoMyPayException
	{
		this.dmpOrderInfoBiz.modityOrderStatus(dmpOrderInfo);
	}
	
	/**
	 * 针对充值信息的错误处理
	 */
	private void dealError(String[] results)
	{
		
	}
}